package tx.yt.charge.web.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import tx.yt.charge.utils.SessionUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: chenmiao@hupu.com
 * Date: 2015-1-8
 */
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse response, Object o) throws Exception {
        String uri = httpServletRequest.getRequestURI();
        /*if (uri.endsWith("/")) {
            //重定向
            response.sendRedirect(uri.substring(0, uri.length() - 1));
            //请求转发
            //httpServletRequest.getRequestDispatcher(uri.substring(0, uri.length() - 1)).forward(httpServletRequest,response);
            return false;
        }*/
        if(uri.endsWith("/login") || uri.endsWith("/loginBs") || uri.contains("/static/"))
            return true;
        if (SessionUtil.isLogin(httpServletRequest))
            return true;
        response.sendRedirect(httpServletRequest.getContextPath() + "/login");
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse response, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse response, Object o, Exception e) throws Exception {

    }
}
